package cn.wangzg.httpurlconndemo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.Map;

import cn.wangzg.httpurlconndemo.utils.HttpCallbackStringListener;
import cn.wangzg.httpurlconndemo.utils.HttpUrlConnectionUtils;

public class LoginActivity extends AppCompatActivity {
    private EditText et_name;
    private EditText et_pwd;
    private CheckBox cb_remPwd;
    private CheckBox cb_showPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
    }

    private void initView() {
        et_name = findViewById(R.id.editText);
        et_pwd = findViewById(R.id.editText2);//密码输入
        cb_remPwd = findViewById(R.id.checkBox);
        cb_showPwd = findViewById(R.id.checkBox2);//显示密码

        cb_showPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });

        SharedPreferences sp=getSharedPreferences("aaa",MODE_PRIVATE);
        String name=sp.getString("name","");
        String pwd=sp.getString("pwd","");
        if(!"".equals(name)){
            et_name.setText(name);
        }
        if(!"".equals(pwd)){
            et_pwd.setText(pwd);
        }
    }

    public void btnLogin(View view){
        //1.获取用户名和密码
        final String name=et_name.getText().toString();
        final String pwd=et_pwd.getText().toString();
        //2.判断是否正确
        Map<String, Object> params=new HashMap<>();
        params.put("name",name);
        params.put("password",pwd);
        HttpUrlConnectionUtils.doPost(this, "http://192.168.2.99:8080/login",
                new HttpCallbackStringListener() {
                    @Override
                    public void onFinish(String response) {
                        if("登录成功".equals(response)){
                            //3.正确，登录，记住密码
                            if(cb_remPwd.isChecked()){
                                SharedPreferences sp=getSharedPreferences("aaa",MODE_PRIVATE);
                                SharedPreferences.Editor edit = sp.edit();
                                edit.putString("name",name);
                                edit.putString("pwd",pwd);
                                edit.commit();
                            }
                            startActivity(new Intent(getApplicationContext(),MainActivity.class));
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                },params);

//        if("admin".equals(name) && "123456".equals(pwd)){
//            if(cb_remPwd.isChecked()){
//                SharedPreferences sp=getSharedPreferences("aaa",MODE_PRIVATE);
//                SharedPreferences.Editor edit = sp.edit();
//                edit.putString("name",name);
//                edit.putString("pwd",pwd);
//                edit.commit();
//            }
//            //startActivity(new Intent(this,));
//            Toast.makeText(this, "success!", Toast.LENGTH_SHORT).show();
//        }

    }
}
